/***************************************
 ***                                 ***
 ***                                 ***
 ***                                 ***
 ***************************************/
 
/** InputForm [静态类页面 获取 / 渲染 数据] */
let InputFormInterface = (function(){
    let InitSign = false;
    // 类的内部方法
    var PrivateClass = {
        description: 'This Method For Private Method For Class',
        /**
         * [ControlModel HTML 控件, 获取器 (页面渲染主控件)]
         * @Author   SEven
         * @DateTime 2019-01-31
         */
        ParentCtrlModel: false,
        RenderingCtrlModel: false,
        ControlModel: function (MainCtrlName = false){
            let MainCtrl = false;

            if(typeof MainCtrlName === 'string'){
                MainCtrl = document.getElementsByClassName(MainCtrlName);
                MainCtrl = MainCtrl.length === 0 ? document.getElementById(MainCtrlName ,true) : MainCtrl;

                if(MainCtrl instanceof HTMLCollection){
                    MainCtrl = MainCtrl[0] === undefined ? false : MainCtrl[0];
                }
            }else if(typeof MainCtrlName === 'object'){
                MainCtrl = MainCtrlName;
            }else if(MainCtrlName === false){
                return [document];
            }

            let ParentCtrlModel = MainCtrl;
            let RenderingCtrlModel = MainCtrl.children[0].cloneNode(true);
            

            if(InitSign === false){
                // 保存 [渲染控件] 父级元素
                PrivateClass.ParentCtrlModel = ParentCtrlModel;
                // 保存 [渲染控件] 模板元素 <复制>
                PrivateClass.RenderingCtrlModel = RenderingCtrlModel.cloneNode(true);

                // 首次进入 ,移除 <页面渲染主控件>
                MainCtrl.removeChild(MainCtrl.children[0]);

                console.log(MainCtrl)
                InitSign = true;
            }


            return [ParentCtrlModel ,RenderingCtrlModel]
        },
    };

    // 主操作渲染类
    var MainRenderingClass = {
        description: 'This Method For Protected Method For Class',
        Rendering: function (ParentCtrlModel ,RenderingCtrlModel ,MainData){
            // 获取 控件 渲染元素主显示集 [主显示控件集]
            let CtrlElems = RenderingCtrlModel.querySelectorAll('div,span,p,input,img,select');
            for(var i=0;i < CtrlElems.length;i++){
                let inputElem = CtrlElems[i];
                let key = inputElem.getAttribute('name');

                // 重新获取最新的数据
                let subGroup = inputElem.getAttribute('name') ? inputElem.getAttribute('name') : "";
                let subGroupKey = MainData[subGroup] ? subGroup : (MainData[subGroup.replace(/-/g ,'_')] ? subGroup.replace(/-/g ,'_') : false);
                
                // 子项内循环渲染入口
                if(subGroupKey !== false && MainData[subGroupKey] instanceof Array) {
                    MainOptionClass.set(inputElem ,MainData[subGroupKey]);
                }else{
                    key = key !== null ? (MainData[key] ? key : (MainData[key.replace(/-/g ,'_')] ? key.replace(/-/g ,'_') : false)) : false;
                    if(key !== false){
                        // 选择显示控件 [ 仅控件显示 / 控件值替换显示 ]
                        let isExists = inputElem.getAttribute('is-exists');
                        let isExistsVal = inputElem.getAttribute('is-exists-val');
                        // 控件类型
                        let TagName = inputElem.tagName;
                        switch(TagName){
                            case 'DIV':
                            case 'SPAN':
                            case 'P':
                                if(isExists && isExists != MainData[key]) inputElem.style.display = 'none';
                                if(isExistsVal && isExistsVal != MainData[key]) inputElem.style.display = 'none';
                                if(!isExists || isExistsVal == MainData[key]) inputElem.innerHTML = MainData[key];
                                break;
                            case 'INPUT':
                            case 'SELECT':
                                if(isExists && isExists != MainData[key]) inputElem.style.display = 'none';
                                if(isExistsVal && isExistsVal != MainData[key]) inputElem.style.display = 'none';
                                if(!isExists || isExistsVal == MainData[key]) inputElem.value = MainData[key];
                                break;
                            case 'IMG':
                                if(isExists && isExists != MainData[key]) inputElem.style.display = 'none';
                                if(isExistsVal && isExistsVal != MainData[key]) inputElem.style.display = 'none';
                                if(!isExists || isExistsVal == MainData[key]) inputElem.src = MainData[key];
                                break;
                        }
                    }
                }
            }

            ParentCtrlModel.appendChild(RenderingCtrlModel);
        },
        /** [setElem 页面单独渲染] */
        setElem: function (MainCtrlName ,MainData){
            var Ctrls = PrivateClass.ControlModel(MainCtrlName);
            let ParentCtrlModel = Ctrls[0];
            let RenderingCtrlModel = Ctrls[1];

            MainRenderingClass.Rendering(ParentCtrlModel ,RenderingCtrlModel ,MainData);
        },
        /** [setGroup 页面循环渲染] */
        setGroup: function (MainCtrlName ,MainData ,overflow = true){
            var Ctrls = PrivateClass.ControlModel(MainCtrlName);
            let ParentCtrlModel = Ctrls[0];
            let RenderingCtrlModel = Ctrls[1];

            if(overflow) ParentCtrlModel.innerHTML = "";

            if(MainData[0]['a1'] == 5){
                console.log(RenderingCtrlModel);
            }
            for(var i=0;i < MainData.length;i++){
                MainRenderingClass.Rendering(ParentCtrlModel ,RenderingCtrlModel.cloneNode(true) ,MainData[i]);
            }
        }
    };

    // 主操作类 [提供外部操作方法]
    var MainOptionClass = {
        description: 'This Method For Get Or Set The Input Value',
        /** [set 设置目标下的所有元素的值] */
        set: function (MainCtrlName ,MainData ,overflow = true){
            return MainData instanceof Array ?
                        MainRenderingClass.setGroup(MainCtrlName ,MainData ,overflow) :
                        MainRenderingClass.setElem(MainCtrlName ,MainData);
        },

        get: function (MainCtrlName = false){
            let Ctrls = PrivateClass.ControlModel(MainCtrlName);
            // 获取当前表单数据
            if(MainCtrlName !== false) Ctrls[0].appendChild(Ctrls[1])
            let ValueModel = Ctrls[0].querySelectorAll("input,select");

            var frmData = new Object();
            for(var i = 0;i < ValueModel.length;i ++){
                let valElem = ValueModel[i];

                if(valElem.getAttribute('post-up') != undefined && valElem.disabled === false){
                    // 可被重写的 KEY 值
                    var setKey = valElem.getAttribute('set-key');
                    var key = setKey != undefined ? setKey.replace(/-/g ,'_').split('.') : valElem.name.replace(/-/g ,'_').split('.');

                    // 数据引导区
                    let ValFrm = frmData;
                    // 多重对象数据层级递进
                    if(key.length > 1){
                        // 层级递进
                        for(var j = 0; j < key.length - 1; j++){
                            ValFrm = ValFrm[key[j]] == undefined ? ValFrm[key[j]] = new Object() : ValFrm[key[j]];
                        }
                    }
                    // 获取最后一项数据键名
                    key = key[key.length - 1];
                    // 数据类型分割
                    if(key.match(/\[\]/)) {
                        // 数组类型
                        key = key.replace(/\[\]/g ,'')
                        ValFrm[key] = ValFrm[key] == undefined ? new Array() : ValFrm[key];
                        ValFrm[key].push(valElem.value)
                    }else{
                        // 普通数据类型
                        ValFrm[key] = valElem.value;
                    }
                }
            }

            return frmData;
        }
    };

    return MainOptionClass;
})();

// 单体具体化类
let InputForm = InputFormInterface;